#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/28 16:01
# @Author  : Andrewq
# @Site    : nope
# @File    : studyHistoryResource.py
# @Software: PyCharm
from flask_restplus import Resource
from . import main_rest_api, back_model
from ..service.studyHistoryManager import studyHistoryManager

history_get_parser = main_rest_api.parser()
history_get_parser.add_argument('userID', type=str, location='args')
history_get_parser.add_argument('tagname', type=str, location='args', required=True)
history_get_parser.add_argument('keyword', type=str, location='args', required=True)

history_delete_parser = main_rest_api.parser()
history_delete_parser.add_argument('userID', type=str, location='form')
history_delete_parser.add_argument('preStudyName', type=str, location='form', required=True)

history_delete_list_parser = main_rest_api.parser()
history_delete_list_parser.add_argument('userID', type=str, location='form')
history_delete_list_parser.add_argument('data', type=str, location='form', required=True)

history_post_parser = main_rest_api.parser()
history_post_parser.add_argument('userID', type=str, location='form', help="缺少userID")
history_post_parser.add_argument('codeID', type=str, location='form', required=True, help="缺少codeID")
history_post_parser.add_argument('infomationID', type=str, location='form', required=True, help="缺少infomationID")
history_post_parser.add_argument('webID', type=str, location='form', required=True, help="缺少webID")
history_post_parser.add_argument('preStudyUrl', type=str, location='form', required=True, help="缺少preStudyUrl")
history_post_parser.add_argument('preStudyName', type=str, location='form', help="缺少preStudyName")

history_get_list_parser = main_rest_api.parser()
history_get_list_parser.add_argument('userID', type=str, location='args')


class studyHistoryResource(Resource):

    @main_rest_api.doc(description="查找历史记录")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(history_get_parser)
    def get(self):
        kwargs = history_get_parser.parse_args()

        result_dict = studyHistoryManager.get_history_info_by_tags(**kwargs)

        return result_dict

    @main_rest_api.doc(description="添加历史记录")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(history_post_parser)
    def post(self):
        kwargs = history_post_parser.parse_args()

        result_dict = studyHistoryManager.add_history(**kwargs)

        return result_dict

    @main_rest_api.doc(description="删除历史记录")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(history_delete_parser)
    def delete(self):
        kwargs = history_delete_parser.parse_args()

        result_dict = studyHistoryManager.delete_history(**kwargs)

        return result_dict


class studyListHistoriesResource(Resource):

    @main_rest_api.doc(description="获取历史记录列表")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(history_get_list_parser)
    def get(self):
        kwargs = history_get_list_parser.parse_args()

        result_dict = studyHistoryManager.get_history_list(**kwargs)

        return result_dict

    @main_rest_api.doc(description="删除历史记录列表")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(history_delete_list_parser)
    def delete(self):
        kwargs = history_delete_list_parser.parse_args()

        result_dict = studyHistoryManager.delete_history_list(**kwargs)

        return result_dict
